<template>
  <div class="changereceipt">
    <van-nav-bar
      title="修改收货地址信息"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-edit
      :addressInfo="addressInfo"
      show-delete
      show-area
      :area-list="areaList"
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
        @delete="onDelete"
    />
  </div>
</template>

<script>
import { Dialog } from "vant";
import { Toast } from "vant";
import areaList from "../../util/area";
import { getreceiptoneapi, updatareceiptapi,delreceiptoneapi } from "../../api/receipt";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addressInfo:{
        name:"",
        tel:"",
        isDefault:false,
        province:"",
        city:"",
        county:"",
        areaCode:"",
      },
      list:{},
    };
  },
  computed: {},
  watch: {},

  methods: {
    //返回上一步
    onClickLeft() {
      Toast("返回");
      this.$router.go(-1);
    },
    // 提交修改信息
    async onSave(obj) {
      console.log(obj);
      const receiver = obj.name;
      const regions = obj.province + "/" + obj.city + "/" + obj.county+"/"+obj.areaCode;
      const address = obj.addressDetail;
      const mobile = obj.tel;
      const idDefault = obj.isDefault;
      this.list = { receiver, regions, address, mobile, idDefault };
      console.log(this.list);
      Dialog.confirm({
        title: "标题",
        message: "是否确认保存",
      })
        .then(() => {
          const id = this.$route.params.id;
          console.log(id);
          const result = updatareceiptapi(id,{params:this.list});
          console.log(result);
          Toast("保存修改信息");
          this.$router.push("/receipt")
        })
        .catch(() => {
          // on cancel
        });
    },

    // 获取地址信息
    async getreceiptone(id) {
      const result = await getreceiptoneapi(id);
      console.log(result);
      
      this.arr=result;
      const name=result.receiver;
      const tel=result.mobile;
      const addressDetail=result.address;
      const isDefault=result.isDefault;
      const ass=result.regions;
      const arr=ass.split("/")
      // console.log(arr);
      // console.log(arr[0]);
      const province=arr[0];
      // console.log(province);
      const city=arr[1];
      const county=arr[2];
      const areaCode=arr[3];
      this.addressInfo={name,tel,addressDetail,isDefault,province,city,county,areaCode}
      // console.log(this.addressInfo);
      
    },
    //删除地址信息
    async onDelete(){
      const id = this.$route.params.id;
      console.log(id);
       const result = await delreceiptoneapi(id);
      console.log(result);
      Toast("删除成功");
      this.$router.push("/receipt")
    }
  },
  created() {
    //查询
    const id = this.$route.params.id;
    console.log(id);
    this.getreceiptone(id);
  },
  mounted() {},
  components: {},
};
</script>
<style scoped></style>
